package com.yangjicong.project.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.List;

@SuppressWarnings("ALL")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FastJsonUtils {

    /**
     * 解析对象为Json字符串
     *
     * @param object
     * @return
     */
    public static String toJsonString(final Object object) {
        return JSON.toJSONString(object);
    }


    /**
     * 解析对象为Json字符串 可自定义解析规则
     *
     * @param object
     * @param features 解析规则
     * @return
     */
    public static String toJsonString(final Object object, SerializerFeature... features) {
        return JSON.toJSONString(object, features);
    }


    /**
     * 解析json字符串到指定类型的对象
     *
     * @param json
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T toObject(String json, final Class<T> valueType) {
        return JSON.parseObject(json, valueType);
    }


    /**
     * 按指定的Type解析json字符串到List
     *
     * @param json
     * @param ObjectType
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(String json, final Class<T> valueType) {
        return JSON.parseArray(json, valueType);
    }


    /**
     * 解析对象为Json字符串
     *
     * @param object
     * @param dateFormatPattern 日期格式，如"yyyy年MM月dd日 HH时mm分ss秒"
     * @return
     */
    public static String toJsonWithDateFormat(final Object object, String dateFormatPattern) {
        return JSON.toJSONStringWithDateFormat(object, dateFormatPattern, SerializerFeature.WriteDateUseDateFormat);
    }






}
